package com.example.a513;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;

import database.BillDBHelper;
import entity.BillInfo;

public class BillAddActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
    private Button btn_sava;
    private EditText et_content;
    private EditText et_money;
    private TextView tv_date;
    private TextView tv_bill;
    private TextView tv_billList;
    private ImageView img_back;
    private RadioGroup rg_group;
    private Calendar calendar;
    private BillDBHelper mHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_bill_add);
        tv_date = findViewById(R.id.tv_date);
        img_back = findViewById(R.id.img_back);
        tv_bill = findViewById(R.id.tv_bill);
        tv_billList = findViewById(R.id.tv_billlist);
        rg_group = findViewById(R.id.rg_group);
        btn_sava = findViewById(R.id.btn_save);
        et_content = findViewById(R.id.et_content);
        et_money = findViewById(R.id.et_money);

        tv_date.setText((LocalDate.now()).toString());
        tv_billList.setText("账单列表");
        tv_bill.setText("请填写账单");
        tv_billList.setOnClickListener(this);
        btn_sava.setOnClickListener(this);
        tv_date.setOnClickListener(this);
        img_back.setOnClickListener(this);
        // 拿到数据库实例
        mHelper = BillDBHelper.getInstance(this);
       mHelper.openReadLink();
       mHelper.openWriteLink();
    }

    @Override
    public void onClick(View v) {
        calendar = Calendar.getInstance();
        if (v.getId() == R.id.btn_save){
            BillInfo info = new BillInfo();
            info.amount = Double.valueOf(et_money.getText().toString()); // 金额
            info.date = tv_date.getText().toString();
            info.type = rg_group.getCheckedRadioButtonId() == R.id.in ? BillInfo.BILL_TYPE_INCOME:BillInfo.BILL_TYPE_OUTCOME;
            info.remark = et_content.getText().toString();
            Log.e("TAG", "onClick: "+info);
            mHelper.save(info);
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();

        }else if (v.getId() == R.id.tv_billlist){
            Intent intent = new Intent();
            intent.setClass(BillAddActivity.this,BuildePagerActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 设置栈顶清空
            startActivity(intent);
        }else if (v.getId() == R.id.tv_date){
            // 弹出日期对话框
             DatePickerDialog dialog = new DatePickerDialog(this, this
                    , calendar.get(Calendar.YEAR)
                    , calendar.get(Calendar.MONTH)
                    , calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        }else if (v.getId() == R.id.img_back){
            finish(); // 结束当前页面
        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            // 给文本设置显示
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month);
        calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);

        final SimpleDateFormat fmat = new SimpleDateFormat("yyyy-MM-dd");
        Log.e("TAG", "onDateSet: "+ calendar.getTime());

        tv_date.setText(fmat.format(calendar.getTime()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.closeLink();
    }
}